// packagePay/pages/billOfPay/billOfPay.js
import {
    shake
} from '../../../utils/util'
import dayjs from 'dayjs'
import {
    circumstancePayment,
    getPaid,
    staffReckoning,
    stopPay,
    getBillList,
    billStatistics,
    getemployeesalaryapi,
    getxmzdlistapi,
    getbillcountapi,
    paybillrecordapi,
    getadvancezdlistapi,
    updateStatusapi,
    getspprocessorderpaybillheadapi,
    spprocessorderpaybillhead
} from '../../../packagePay/api/pay'
import user from '../../../utils/user.js'
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        showtip: true,
        array: ['全部', '待领薪', '已领薪', '停止发薪', '进行中', '领薪失败'],
        zdmxstatus: 0,
        listtabarr: [{
            name: '已领薪',
            index: 0,
            active: true,
            value: 1
        }, {
            name: "待领薪",
            index: 1,
            active: false,
            value: 0
        }, {
            name: "停止发薪",
            index: 2,
            active: false,
            value: 2
        }, {
            name: "领薪失败",
            index: 3,
            active: false,
            value: 4
        }, {
            name: "进行中",
            index: 4,
            active: false,
            value: 3
        }],
        data0: [],
        data1: [],
        data2: [],
        data3: [],
        advancesearchtext: '',
        projectMonth0: "",
        projectMonth1: "",
        projectMonth2: "",
        projectMonth3: "",
        seekItemTextzdmx: "",
        seekItemTextygzd: "",
        seekItemTextxmzd: "",
        fxmx1: '',
        fxmx2: '',
        fxmx3: '',
        num1: '',
        num2: '',
        num3: '',
        tabs: 0,
        arr: [{
                name: '发薪账单',
                index: 0
            },
            {
                name: '预支账单',
                index: 2
            },
            {
                name: '账单总计',
                index: 1
            },
        ],
        taboneallMoney: '',
        tabonealrMoney: "",
        tabonewaitMoney: "",
        taboneallMoney1: '',
        tabonealrMoney1: "",
        tabonewaitMoney1: "",
        taboneallMoney2: '',
        tabonealrMoney2: "",
        tabonewaitMoney2: "",
        taboneallMoney3: '',
        tabonealrMoney3: "",
        tabonewaitMoney3: "",
        tabonehasnextpage: true,
        tabtwohasnextpage: true,
        tabthreehasnextpage: true,
        tabfourhasnextpage: true,
        data0pagenum: 1,
        data1pagenum: 1,
        data2pagenum: 1,
        data3pagenum: 1,
        allpagesize: 10,
        isSeatAdmin: null,
        hidemonth1: false,
        hidemonth2: false,
        hidemonth3: false,
        hidemonth4: false,
        innertabflag: 0
    },

    /**
     * 生命周期函数--监听页面加载
     */
    showtipkg() {
        this.setData({
            showtip: !this.data.showtip
        })
    },
    toadvancedetail(e) {
        console.log(e);
        wx.navigateTo({
            url: '../../../packageAdvance/pages/advanceDetail/advanceDetail?id=' + e.currentTarget.dataset.id + '&istwo=1',
        })
    },
    changeinnertab(e) {
        console.log(e, 'eeeeeeeeeeeeeeeeeeee');
        this.setData({
            innertabflag: e.currentTarget.dataset.i * 1
        })
        if (this.data.innertabflag === 0) {
            this.setData({
                data0: [],
                data0pagenum: 1,
            }, () => {
                this.getbillcount()
                this.getxmzdlist()
            })
        } else {
            this.setData({
                data2: [],
                data2pagenum: 1,
            }, () => {
                this.paybillrecord()
                spprocessorderpaybillhead({
                    queryMonth: this.data.projectMonth2
                }).then((res) => {
                    console.log(res, 'shuyig');
                    this.setData({
                        num1: res.paymentAmount,
                        num2: res.alrPaymentAmount,
                        num3: res.unclaimedAmount,
                    })
                })
            })
        }
    },
    bindPickerChange: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            zdmxstatus: e.detail.value * 1
        })
        this.setData({
            data2: [],
            data2pagenum: 1,
            listtabarr: this.data.listtabarr
        })
        this.paybillrecord()
    },
    bindPickerChangeccc: function (e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
            zdmxstatus: e.detail.value * 1
        })
        this.setData({
            data3: [],
            data3pagenum: 1,
        })
        this.getadvancezdlist()
    },
    onLoad(options) {
        this.setData({
            projectMonth0: dayjs().format('YYYY-MM'),
            projectMonth1: dayjs().format('YYYY-MM'),
            projectMonth2: dayjs().format('YYYY-MM'),
            projectMonth3: dayjs().format('YYYY-MM'),
        })
        user.waitLogin().then(() => {
            this.setData({
                projectMonth0: dayjs().format('YYYY-MM'),
                projectMonth1: dayjs().format('YYYY-MM'),
                projectMonth3: dayjs().format('YYYY-MM'),
                projectMonth2: dayjs().format('YYYY-MM'),
                isSeatAdmin: user.isSeatAdmin
            }, () => {
                this.getbillcount()
                this.getxmzdlist()
            })
        })
    },
    paybillrecord() {
        // this.data.listtabarr.forEach((ele, i) => {
        //     if (ele.active) {
        //         this.setData({
        //             zdmxstatus: ele.value
        //         })
        //     }
        // })
        let obj = {}
        if (this.data.hidemonth3) {
            obj = {
                pageNum: this.data.data2pagenum,
                pageSize: this.data.allpagesize,
                // queryMonth: this.data.projectMonth2,
                searchText: this.data.seekItemTextzdmx,
                status: this.data.zdmxstatus === 0 ? '' : this.data.zdmxstatus - 1
            }
        } else {
            obj = {
                pageNum: this.data.data2pagenum,
                pageSize: this.data.allpagesize,
                queryMonth: this.data.projectMonth2,
                searchText: this.data.seekItemTextzdmx,
                status: this.data.zdmxstatus === 0 ? '' : this.data.zdmxstatus - 1
            }
        }
        paybillrecordapi({...obj,audit_status:1}).then((res) => {
            console.log(res, 'list');
            if (res.list) {
                res.list.forEach((ele, i) => {
                    if (ele.idCardNo) {
                        ele.idCardNo4 = ele.idCardNo.substr(-4)
                    } else {
                        ele.idCardNo4 = '--'
                    }
                })
            }
            if (!res.hasNextPage) {
                this.data.tabthreehasnextpage = false
            } else {
                this.data.tabthreehasnextpage = true
            }
            let arr = []
            arr.push(...this.data.data2)
            arr.push(...res.list)
            this.setData({
                data2: arr,
                tabthreehasnextpage: res.hasNextPage,
            })
        })
    },
    onStopadvance(e) {
        app.confirm('是否确认停止该员工预支').then(() => {
            updateStatusapi({
                id: e.currentTarget.dataset.id
            }).then(() => {
                app.toast('停止发薪成功', 'success')
                setTimeout(() => {
                    this.setData({
                        data3: [],
                        data3pagenum: 1,
                    }, () => {
                        this.getadvancezdlist()
                    })
                }, 1000)
            })
        })
    },
    onStop(event) {
        app.confirm('是否确认停止该员工发薪').then(() => {
            stopPay({
                id: event.currentTarget.dataset.id
            }).then(() => {
                app.toast('停止发薪成功', 'success')
                setTimeout(() => {
                    this.setData({
                        data2: [],
                        data2pagenum: 1,
                    }, () => {
                        this.paybillrecord()
                        getemployeesalaryapi({
                            queryMonth: this.data.projectMonth2,
                        }).then((res) => {
                            this.setData({
                                taboneallMoney2: res.header.allRealHairAmount,
                                tabonealrMoney2: res.header.allReceived,
                                tabonewaitMoney2: res.header.allUnclaimed,
                            })
                        })
                    })
                }, 1000)

            })
        })
    },
    getemployeesalary() {
        let obj = {}
        if (this.data.hidemonth2) {
            obj = {
                pageNum: this.data.data1pagenum,
                pageSize: this.data.allpagesize,
                // queryMonth: this.data.projectMonth1,
                searchText: this.data.seekItemTextygzd,
            }
        } else {
            obj = {
                pageNum: this.data.data1pagenum,
                pageSize: this.data.allpagesize,
                queryMonth: this.data.projectMonth1,
                searchText: this.data.seekItemTextygzd,
            }
        }
        getemployeesalaryapi(obj).then((res) => {
            console.log(res, 'list');
            if (!res.hasNextPage) {
                this.data.tabtwohasnextpage = false
            } else {
                this.data.tabtwohasnextpage = true
            }
            res.list.forEach(ele => {
                if (ele.idCardNo) {
                    ele.idCardNo4 = ele.idCardNo.substr(-4)
                } else {
                    ele.idCardNo4 = '--'
                }
            })
            let arr = []
            arr.push(...this.data.data1)
            arr.push(...res.list)
            this.setData({
                data1: arr,
                tabtwohasnextpage: res.hasNextPage,
                taboneallMoney1: res.header.alrPaymentAmount,
                tabonealrMoney1: res.header.paymentAmount,
                tabonewaitMoney1: res.header.unclaimedAmount,
            })
        })
    },
    ygzdcall(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.num,
        })
    },
    getxmzdlist() {
        let obj = {}
        if (this.data.hidemonth1) {
            obj = {
                // month: this.data.projectMonth0,
                pageSize: this.data.allpagesize,
                pageNum: this.data.data0pagenum,
                projectName: this.data.seekItemTextxmzd,
                queryTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss.SSS')
            }
        } else {
            obj = {
                month: this.data.projectMonth0,
                pageSize: this.data.allpagesize,
                pageNum: this.data.data0pagenum,
                projectName: this.data.seekItemTextxmzd,
                queryTime: dayjs(new Date()).format('YYYY-MM-DD HH:mm:ss.SSS')
            }
        }
        getxmzdlistapi(obj).then((res) => {
            console.log(res, 'list');
            if (!res.hasNextPage) {
                this.data.tabonehasnextpage = false
            } else {
                this.data.tabonehasnextpage = true
            }
            let arr = []
            arr.push(...this.data.data0)
            arr.push(...res.list)
            this.setData({
                data0: arr,
                tabonehasnextpage: res.hasNextPage
            })
        })
    },
    async getbillcount() {
        const result = await getbillcountapi({
            queryMonth: this.data.projectMonth0
        })
        console.log(result, 'result166')
        this.setData({
            tabonewaitMoney: result.waitPayAmount,
            tabonealrMoney: result.totalPayAmount,
            taboneallMoney: result.realHairAmount,
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },
    onSkip(e) {
        console.log(e);
        wx.navigateTo({
            url: '/packagePay/pages/projectbill/projectbill?id=' + e.currentTarget.dataset.id + '&month=' + this.data.projectMonth0,
        })
    },
    onSkiptwo(e) {
        console.log(e, 'huuiijudfgs');
        wx.navigateTo({
            url: '/packagePay/pages/employeebill/employeebill?id=' + e.currentTarget.dataset.id + '&idCardNo=' + e.currentTarget.dataset.idcardno + '&month=' + this.data.projectMonth1,
        })
    },
    onJump(event) {
        console.log(event, 'onJumponJump')
        // if (event.currentTarget.dataset.status !== 0) {
        let id = event.currentTarget.dataset.id
        wx.navigateTo({
            url: '/packagePay/pages/individual-detail/individual-detail?id=' + id,
        })
        // }
    },
    changelisttab(e) {
        console.log(e.currentTarget.dataset.index);
        this.data.listtabarr.forEach((ele, index) => {
            ele.active = false
        })
        let i = e.currentTarget.dataset.index
        this.data.listtabarr[i].active = !this.data.listtabarr[i].active
        console.log(this.data.listtabarr);
        this.setData({
            data2: [],
            data2pagenum: 1,
            listtabarr: this.data.listtabarr
        })
        this.paybillrecord()
    },
    zdmxcall(e) {
        wx.makePhoneCall({
            phoneNumber: e.currentTarget.dataset.num,
        })
    },
    onChange(event) {
        console.log(event.currentTarget.dataset.index, '哈哈哈哈哈哈哈')
        let i = event.currentTarget.dataset.index
        this.setData({
            tabs: i
        })
        if (i === 0) {
            if (this.data.innertabflag === 0) {
                this.setData({
                    data0: [],
                    data0pagenum: 1,
                }, () => {
                    this.getbillcount()
                    this.getxmzdlist()
                })
            } else {
                this.setData({
                    data2: [],
                    data2pagenum: 1,
                }, () => {
                    this.paybillrecord()
                    spprocessorderpaybillhead({
                        queryMonth: this.data.projectMonth2
                    }).then((res) => {
                        console.log(res, 'shuyig');
                        this.setData({
                            num1: res.paymentAmount,
                            num2: res.alrPaymentAmount,
                            num3: res.unclaimedAmount,
                        })
                    })
                })
            }
        } else if (i === 1) {
            wx.reportEvent("click_employee_bill_num", {
                "click_num": "true"
            })
            this.setData({
                data1: [],
                data1pagenum: 1,
            }, () => {
                this.getemployeesalary()
            })
        } else {
            this.setData({
                data3: [],
                data3pagenum: 1,
            }, () => {
                // 预支接口调用
                this.getadvancezdlist()
                getspprocessorderpaybillheadapi({
                    queryApplyMonth: this.data.projectMonth3,
                }).then((res) => {
                    this.setData({
                        fxmx1: res.allAmount,
                        fxmx2: res.alrAmount,
                        fxmx3: res.aitAmount,
                    })
                })
            })
        }

    },
    getadvancezdlist() {
        getadvancezdlistapi({
            status: this.data.zdmxstatus === 0 ? '' : this.data.zdmxstatus - 1,
            searchText: this.data.advancesearchtext,
            queryApplyMonth: this.data.projectMonth3,
            pageNum: this.data.data3pagenum,
            pageSize: this.data.allpagesize,
        }).then((res) => {
            console.log(res);
            if (res.list) {
                res.list.forEach((ele, i) => {
                    ele.idCardNo = ele.idCardNo.slice(-4)
                    
                })
            }
            let arr = this.data.data3.concat(res.list)
            this.setData({
                data3: arr,
                tabfourhasnextpage: res.hasNextPage
            })
        })
    },
    onSeekItemInput0(event) {
        console.log(event, '搜索inputinput')
        if (event.detail.value) {
            console.log(222);
            this.setData({
                hidemonth1: true
            })
        } else {
            this.setData({
                hidemonth1: false
            })
        }
        shake(() => {
            this.setData({
                data0: [],
                data0pagenum: 1,
                tabonehasnextpage: true
            })
            this.getxmzdlist()
        }, 1000)
    },
    onSeekItemInput1(event) {
        if (event.detail.value) {
            this.setData({
                hidemonth2: true
            })
        } else {
            this.setData({
                hidemonth2: false
            })
        }
        shake(() => {
            this.setData({
                data1: [],
                data1pagenum: 1,
                tabtwohasnextpage: true
            })
            this.getemployeesalary()
        }, 1000)
    },
    onSeekItemInput2(event) {
        if (event.detail.value) {
            this.setData({
                hidemonth3: true
            })
        } else {
            this.setData({
                hidemonth3: false
            })
        }
        shake(() => {
            this.setData({
                data2: [],
                data2pagenum: 1,
                tabthreehasnextpage: true
            })
            this.paybillrecord()
        }, 1000)
    },
    onSeekItemInput3(event) {
        if (event.detail.value) {
            this.setData({
                hidemonth4: true
            })
        } else {
            this.setData({
                hidemonth4: false
            })
        }
        shake(() => {
            this.setData({
                data3: [],
                data3pagenum: 1,
                tabfourhasnextpage: true
            })
            this.getadvancezdlist()
        }, 1000)
    },
    onProjectMonthChange0(event) {
        this.setData({
            projectMonth0: event.detail.value,
            data0: [],
            data0pagenum: 1,
            tabonehasnextpage: true
        })
        this.getxmzdlist()
        this.getbillcount()
    },
    onProjectMonthChange1(event) {
        this.setData({
            projectMonth1: event.detail.value,
            data1: [],
            data1pagenum: 1,
            tabtwohasnextpage: true
        })
        this.getemployeesalary()
    },
    onProjectMonthChange2(event) {
        this.setData({
            projectMonth2: event.detail.value,
            data2: [],
            data2pagenum: 1,
            tabthreehasnextpage: true
        })
        this.paybillrecord()
        spprocessorderpaybillhead({
            queryMonth: this.data.projectMonth2
        }).then((res) => {
            console.log(res, 'shuyig');
            this.setData({
                num1: res.paymentAmount,
                num2: res.alrPaymentAmount,
                num3: res.unclaimedAmount,
            })
        })
    },
    onProjectMonthChange3(event) {
        this.setData({
            projectMonth3: event.detail.value,
            data3: [],
            data3pagenum: 1,
            tabfourhasnextpage: true
        })
        this.getadvancezdlist()
        getspprocessorderpaybillheadapi({
            queryApplyMonth: this.data.projectMonth3,
        }).then((res) => {
            this.setData({
                fxmx1: res.allAmount,
                fxmx2: res.alrAmount,
                fxmx3: res.aitAmount,
            })
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {
        console.log(111);
        if (this.data.tabs === 0) {
            if (this.data.innertabflag === 0 && this.data.tabonehasnextpage) {
                this.setData({
                    data0pagenum: this.data.data0pagenum + 1
                }, () => {
                    this.getxmzdlist()
                })
            } else if (this.data.innertabflag === 1 && this.data.tabthreehasnextpage) {
                this.setData({
                    data2pagenum: this.data.data2pagenum + 1
                }, () => {
                    this.paybillrecord()
                })
            }

        } else if (this.data.tabs === 1 && this.data.tabtwohasnextpage) {
            this.setData({
                data1pagenum: this.data.data1pagenum + 1
            }, () => {
                this.getemployeesalary()
            })
        } else if (this.data.tabs === 2 && this.data.tabfourhasnextpage) {
            console.log(222);
            this.setData({
                data3pagenum: this.data.data3pagenum + 1
            }, () => {
                // 预支接口调用
                this.getadvancezdlist()
            })
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})